#! /usr/bin/env python
import os
import re

_DEBUG = True
_DEBUG = False
if _DEBUG:
    import pdb
    pdb.set_trace()

# create empty list for file and filename
flist = list()
olist = list()

# read file name list 
ret = os.popen('ls *.txt')
for fn in ret:
    # open num.txt for read
    flist.append(open(fn.strip(), "r"))
    # open num.dat for write
    olist.append(open(fn.strip().replace('.txt', '.dat'), "w"))

# travel flles and make statistics
for i, f in enumerate(flist):
    olist[i].write('ofcount rlcount\n')
    for t in range(100):
        ofcount = 0
        rlcount = 0
        for m in range(100):
            line = f.next()
            expr = r'domid: \d+, Overflow: (?P<overflow>\d), RQlen: +(?P<rqlen>\d+)$'
            m = re.search(expr, line)
            overflow = int(m.group('overflow'))
            rqlen = int(m.group('rqlen'))
            if overflow == 1:
                ofcount += 1
            if rqlen > 128:
                rlcount += 1
        # write overflow and rqlen count
        olist[i].write('%d %d\n' %(ofcount, rlcount))
    # close num.dat
    olist[i].close()
    flist[i].close()
